package util.fastDfs;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.IOException;

/**
 * Created by sc on 2017/10/11.
 */
public class FastDFSClient {

    private TrackerClient trackerClient = null;
    private TrackerServer trackerServer = null;
    private StorageServer storageServer = null;
    private StorageClient1 storageClient = null;

    public FastDFSClient(String conf) throws IOException, MyException {
        if(conf.contains("classpath:")){
            conf = conf.replace("classpath:",this.getClass().getResource("/").getPath());
        }
        ClientGlobal.init(conf);
        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
        storageServer = null;
        storageClient = new StorageClient1(trackerServer,storageServer);
    }

    /***
     *
     * @param filename
     * @param extname
     * @param metas
     * @return
     */
    public String uploadFile(String filename, String extname, NameValuePair[] metas) throws IOException, MyException {
       String result = storageClient.upload_appender_file1(filename,extname,metas);
        return result;
    }

    public String uploadFile(String filename) throws IOException, MyException {
        return uploadFile(filename,null,null);
    }

    public String uploadFile(String filename,String extname) throws IOException, MyException {
        return uploadFile(filename,extname,null);
    }


    /***
     *
     * @param fileContent
     * @param extName
     * @param metas
     * @return
     * @throws IOException
     * @throws MyException
     */
    public String uploadFile(byte[] fileContent,String extName,NameValuePair[] metas) throws IOException, MyException {
        String result = storageClient.upload_file1(fileContent,extName,metas);
        return result;
    }

    public String uploadFile(byte[] fileContent) throws IOException, MyException {
        String result = uploadFile(fileContent,null,null);
        return result;
    }
    public String uploadFile(byte[] fileContent,String extname) throws IOException, MyException {
        String result = uploadFile(fileContent,extname,null);
        return result;
    }

}
